<%
Class FastAsp_Log
    Private m_Path       '日志路径
    Private m_File       '日志文件
    Private m_Level      '日志级别
    Private m_MaxSize    '最大大小(MB)
    Private m_Stream     '文件流
    
    Private Sub Class_Initialize()
        m_Path = GetConfig("LogPath")
        If m_Path = "" Then m_Path = "/runtime/logs"
        m_Level = GetConfig("LogLevel")
        If m_Level = "" Then m_Level = "debug"
        m_MaxSize = GetConfig("LogMaxSize")
        If m_MaxSize = "" Then m_MaxSize = 10
        
        '确保日志目录存在
        CreateFolder Server.MapPath(m_Path)
    End Sub
    
    '写入调试日志
    Public Function Debug(byval msg)
        WriteLog "DEBUG", msg
        Set Debug = Me
    End Function
    
    '写入信息日志
    Public Function Info(byval msg)
        WriteLog "INFO", msg
        Set Info = Me
    End Function
    
    '写入警告日志
    Public Function Warn(byval msg)
        WriteLog "WARN", msg
        Set Warn = Me
    End Function
    
    '写入错误日志
    Public Function Error_(byval msg)
        WriteLog "ERROR", msg
        Set Error_ = Me
    End Function
    
    '写入SQL日志
    Public Function SQL(byval sqlStr)
        WriteLog "SQL", sqlStr
        Set SQL = Me
    End Function
    
    '写入日志
    Private Sub WriteLog(byval level, byval msg)
        '检查日志级别
        If Not CheckLevel(level) Then Exit Sub
        
        '设置日志文件
        m_File = m_Path & "/" & Year(Now()) & Month(Now()) & Day(Now()) & ".log"
        
        '打开文件流
        OpenStream()
        
        '写入日志
        m_Stream.WriteText "[" & Now() & "][" & level & "] " & msg & vbCrLf
        
        '关闭文件流
        m_Stream.Close
        Set m_Stream = Nothing
    End Sub
    
    '检查日志级别
    Private Function CheckLevel(byval level)
        Select Case UCase(m_Level)
            Case "DEBUG" CheckLevel = True
            Case "INFO"  CheckLevel = (level <> "DEBUG")
            Case "WARN"  CheckLevel = (level <> "DEBUG" And level <> "INFO")
            Case "ERROR" CheckLevel = (level = "ERROR")
            Case Else    CheckLevel = False
        End Select
    End Function
    
    '打开文件流
    Private Sub OpenStream()
        '检查文件大小
        Dim fso : Set fso = Server.CreateObject("Scripting.FileSystemObject")
        Dim file : Set file = fso.GetFile(Server.MapPath(m_File))
        
        '如果超过最大大小，备份当前文件
        If file.Size > m_MaxSize * 1024 * 1024 Then
            Dim bakFile : bakFile = Left(m_File, InStrRev(m_File, ".") - 1) & "_" & _
                Hour(Now()) & Minute(Now()) & Second(Now()) & ".log"
            file.Move Server.MapPath(bakFile)
        End If
        
        '打开文件流
        Set m_Stream = CreateObject("ADODB.Stream")
        m_Stream.Type = 2 'adTypeText
        m_Stream.Charset = "utf-8"
        m_Stream.Open
        
        '如果文件存在则追加，否则创建
        If fso.FileExists(Server.MapPath(m_File)) Then
            m_Stream.LoadFromFile Server.MapPath(m_File)
            m_Stream.Position = m_Stream.Size
        End If
    End Sub
    
    '创建文件夹
    Private Sub CreateFolder(byval path)
        Dim fso : Set fso = Server.CreateObject("Scripting.FileSystemObject")
        If Not fso.FolderExists(path) Then
            CreateFolder fso.GetParentFolderName(path)
            fso.CreateFolder path
        End If
    End Sub
End Class
%> 